package com.meme.editor.gui.level;

import java.awt.Dimension;
import java.awt.event.FocusListener;
import java.awt.event.KeyListener;

import javax.swing.JDialog;
import javax.swing.JTabbedPane;

import com.meme.editor.gui.PropertyEditPanel;
import com.meme.editor.level.Actor;
import com.meme.editor.level.Appearance;

import com.meme.util.GuiUtil;

import java.util.List;

public class ActorSetupDialog extends JDialog 
             implements FocusListener, KeyListener
{

	JTabbedPane mTabbedPanel;
	
	LevelMainView mLevelMainView;
	
	public ActorSetupDialog (LevelMainView levelMainView)
	{
		super ( GuiUtil.getTopFrame (levelMainView) );
		
		setLevelMainView (levelMainView);
		
		setMinimumSize (_lastDim);
		setFocusable (true);
		addFocusListener (this);
		addKeyListener (this);		
		
		mTabbedPanel = new JTabbedPane ();
		add (mTabbedPanel);
	}
	
	public void setLevelMainView (LevelMainView view)
	{
		mLevelMainView = view;
	}
	
	Dimension _lastDim = new Dimension (200, 300);
	public void setActor (Actor actor)
	{
		int tabs = mTabbedPanel.getTabCount();
		
		//Dimension dialogDim = getSize ();
		Dimension dim = null;
		if (tabs > 0)
			dim = mTabbedPanel.getComponentAt(0).getSize();
		else
			dim = _lastDim;

		int curIndex = mTabbedPanel.getSelectedIndex();
		
		mTabbedPanel.removeAll();
		
		
		if (actor == null)
		{
			_lastDim = dim;
			//setPreferredSize (new Dimension(dialogDim));
		}
		else
		{
			PropertyEditPanel propertiesPanel = new PropertyEditPanel (null);
			if (dim != null)
				propertiesPanel.setPreferredSize(dim);		

			mTabbedPanel.add ("Properties", propertiesPanel);
			pack ();

			propertiesPanel.setDataSource (actor);

			List <Appearance> appearances = actor.getAppearanceList ();
			
			int altId = 0;
			
			for (Appearance appearance : appearances)
			{
				PropertyEditPanel appearancePanel = new PropertyEditPanel (null);
				if (dim != null)
					appearancePanel.setPreferredSize(dim);	
				
				if (appearance.isGeometryAppearance())
				{
					mTabbedPanel.add ("[" + (altId ++) + "] " + appearance.getName(), appearancePanel);					
				}
				else
				{
					mTabbedPanel.add (appearance.getName(), appearancePanel);					
				}
				
				pack ();
				
				appearancePanel.setDataSource(appearance);
			}
		}
		
//		for (int i = 0; i < tabs;  ++ i)
//			mTabbedPanel.remove (i);
		
		if (curIndex >= 0 && mTabbedPanel.getTabCount() > curIndex)
			mTabbedPanel.setSelectedIndex(curIndex);
		
		setTitle (actor == null ? "" : "Actor #" + actor.getIndex() + ": " + actor.getName());
	}
	
//--------------------------------------------------------------------------
// as a KeyListener
//--------------------------------------------------------------------------

	  // Method descriptor #10 (Ljava/awt/event/KeyEvent;)V
	  public void keyTyped(java.awt.event.KeyEvent e)
	  {
		  
	  }
	  
	  // Method descriptor #10 (Ljava/awt/event/KeyEvent;)V
	  public void keyPressed(java.awt.event.KeyEvent e)
	  {
		  switch (e.getKeyCode())
		  {
		  default:
			  mLevelMainView.keyPressed (e);		
		  }
	  }
	  
	  // Method descriptor #10 (Ljava/awt/event/KeyEvent;)V
	  public void keyReleased(java.awt.event.KeyEvent e)
	  {
		  
	  }

//--------------------------------------------------------------------------
// as a FocusListener
//--------------------------------------------------------------------------

	  
	  // Method descriptor #9 (Ljava/awt/event/FocusEvent;)V
	  public void focusGained(java.awt.event.FocusEvent e)
	  {
		  return;
	  }
	  
	  // Method descriptor #9 (Ljava/awt/event/FocusEvent;)V
	  public void focusLost(java.awt.event.FocusEvent e)
	  {
		  return;
	  }	

}
